package com.example.controller;

import com.example.domain.Comment;
import com.example.domain.User;
import com.example.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
//处理与评论相关的请求
@Controller
public class CommentController {

    @Autowired
    private CommentService commentService;

    @GetMapping("/comments")
    @ResponseBody
    public List<Comment> getComments(@RequestParam Integer postId) {
        return commentService.getCommentsByPostId(postId);
    }

    @PostMapping("/comment/save")
    public String saveComment(Comment comment,
                              @RequestParam(required = false) Integer parentId,
                              HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("USER_SESSION");
        if (user == null) {
            request.setAttribute("msg", "请先登录！");
            return "login";
        }

        comment.setUserId(user.getId());
        comment.setParentId(parentId); // 设置父评论ID
        comment.setStatus("active");
        comment.setCreateTime(new Date());

        commentService.save(comment);

        // 重定向回帖子详情页，如果是回复则跳转到对应评论
        String redirectUrl = "redirect:/postDetail?id=" + comment.getPostId();
        if (parentId != null) {
            redirectUrl += "#comment-" + parentId;
        }
        return redirectUrl;
    }

    @PostMapping("/comment/ajaxSave")
    @ResponseBody
    public String ajaxSaveComment(Comment comment,
                                  @RequestParam(required = false) Integer parentId,
                                  HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("USER_SESSION");
        if (user == null) {
            return "<p style='color:red;'>请先登录！</p>";
        }

        comment.setUserId(user.getId());
        comment.setParentId(parentId);
        comment.setStatus("active");
        comment.setCreateTime(new Date());

        commentService.save(comment);

        // 构建返回 HTML
        String replyForm = "";
        if (parentId == null) { // 只有顶级评论才显示回复表单
            replyForm = "<form action='/comment/save' method='post'>"
                    + "<input type='hidden' name='postId' value='" + comment.getPostId() + "'/>"
                    + "<input type='hidden' name='parentId' value='" + comment.getId() + "'/>"
                    + "<textarea name='content' placeholder='回复该评论...' required></textarea><br>"
                    + "<input type='submit' value='回复'/>"
                    + "</form>";
        }

        return "<div style='margin-top: 15px; margin-left: " + (parentId != null ? "40px" : "0px") + ";'>"
                + "<p><strong>" + (user.getUsername() != null ? user.getUsername() : "用户" + user.getId()) + "</strong> "
                + new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(comment.getCreateTime()) + "</p>"
                + "<p>" + comment.getContent() + "</p>"
                + replyForm
                + "</div>";
    }
}